<template>
  <el-dialog :visible.sync="show" title="分配权限" @close="closeEvent">
    <el-tree
      ref="tree"
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      node-key="id"
      :default-checked-keys="permIds"
      check-strictly
    />
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="show = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleById, sysRoleAssignPrem } from '@/api/setting'
export default {
  data() {
    return {
      show: false,
      list: [],
      permIds: [],
      id: ''
    }
  },
  methods: {
    async getPerList() {
      // 0===>false  其它转换为true
      if (this.list.length) {
        return
      }
      const res = await sysPermission()
      this.list = changeData(res.data, '0')
      console.log('权限列表数据', res)
    },
    // 获取角色详情
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleById(id)
      this.permIds = res.data.permIds
      //   this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log('角色详情数据', res)
    },
    closeEvent() {
      this.$refs.tree.setCheckedKeys([])
    },
    // 确定点击事件
    async submit() {
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配权限成功')
      this.show = false
    }
  }
}
</script>
<style></style>
